package org.example.lethear.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.example.lethear.dto.common.PageResult;
import org.example.lethear.dto.report.ReportCreateRequest;
import org.example.lethear.dto.report.ReportDTO;
import org.example.lethear.dto.report.ReportHandleRequest;
import org.example.lethear.service.ReportService;
import org.example.lethear.utils.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 举报管理控制器
 */
@Tag(name = "举报管理", description = "举报功能相关接口")
@RestController
@RequestMapping("/reports")
@RequiredArgsConstructor
public class ReportController {
    
    private final ReportService reportService;
    
    /**
     * 创建举报
     */
    @Operation(summary = "创建举报", description = "用户举报内容")
    @PostMapping
    public Result<ReportDTO> createReport(@Valid @RequestBody ReportCreateRequest request) {
        ReportDTO report = reportService.createReport(request);
        return Result.success("举报提交成功", report);
    }
    
    /**
     * 获取用户的举报记录
     */
    @Operation(summary = "获取用户举报记录", description = "获取当前用户的举报记录")
    @GetMapping("/my")
    public Result<PageResult<ReportDTO>> getUserReports(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") int page,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") int size) {
        PageResult<ReportDTO> reports = reportService.getUserReports(page, size);
        return Result.success("获取成功", reports);
    }
    
    /**
     * 获取举报详情
     */
    @Operation(summary = "获取举报详情", description = "获取指定举报记录的详细信息")
    @GetMapping("/{reportId}")
    public Result<ReportDTO> getReportById(@Parameter(description = "举报ID") @PathVariable Long reportId) {
        ReportDTO report = reportService.getReportById(reportId);
        return Result.success("获取成功", report);
    }
    
    /**
     * 获取举报列表（管理员功能）
     */
    @Operation(summary = "获取举报列表", description = "管理员获取举报列表")
    @GetMapping("/admin")
    @PreAuthorize("hasAuthority('ROLE_ADMIN') or hasAuthority('ROLE_SYSTEM_ADMIN')")
    public Result<PageResult<ReportDTO>> getReports(
            @Parameter(description = "状态筛选") @RequestParam(required = false) Integer status,
            @Parameter(description = "目标类型筛选") @RequestParam(required = false) Integer targetType,
            @Parameter(description = "举报原因类型筛选") @RequestParam(required = false) Integer reasonType,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") int page,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") int size) {
        PageResult<ReportDTO> reports = reportService.getReports(status, targetType, reasonType, page, size);
        return Result.success("获取成功", reports);
    }
    
    /**
     * 获取待处理举报列表（管理员功能）
     */
    @Operation(summary = "获取待处理举报", description = "管理员获取待处理的举报列表")
    @GetMapping("/admin/pending")
    @PreAuthorize("hasAuthority('ROLE_ADMIN') or hasAuthority('ROLE_SYSTEM_ADMIN')")
    public Result<PageResult<ReportDTO>> getPendingReports(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") int page,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") int size) {
        PageResult<ReportDTO> reports = reportService.getPendingReports(page, size);
        return Result.success("获取成功", reports);
    }
    
    /**
     * 处理举报（管理员功能）
     */
    @Operation(summary = "处理举报", description = "管理员处理举报记录")
    @PutMapping("/{reportId}/handle")
    @PreAuthorize("hasAuthority('ROLE_ADMIN') or hasAuthority('ROLE_SYSTEM_ADMIN')")
    public Result<ReportDTO> handleReport(
            @Parameter(description = "举报ID") @PathVariable Long reportId,
            @Valid @RequestBody ReportHandleRequest request) {
        ReportDTO report = reportService.handleReport(reportId, request);
        return Result.success("处理成功", report);
    }
    
    /**
     * 获取举报统计信息（管理员功能）
     */
    @Operation(summary = "获取举报统计", description = "管理员获取举报统计信息")
    @GetMapping("/admin/stats")
    @PreAuthorize("hasAuthority('ROLE_ADMIN') or hasAuthority('ROLE_SYSTEM_ADMIN')")
    public Result<ReportService.ReportStatsDTO> getReportStats() {
        ReportService.ReportStatsDTO stats = reportService.getReportStats();
        return Result.success("获取成功", stats);
    }
} 